const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
const api = require('./api.js');
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

/**
 * 封封微信的的request
 */
function request(url, data = {}, method = "GET") {
    wx.showLoading({
        title: '努力加载中',
      })
  return new Promise(function(resolve, reject) {
      wx.request({
          url: url,
          data: data,
          method: method,
          header: {
              'Content-Type': 'application/json',
              'X-Hioshop-Token': wx.getStorageSync('token')
          },
          success: function(res) {
              if (res.statusCode == 200) {
                  if (res.data.errno == 401) {
                      //需要登录后才可以操作

                      // let code = null;
                      // return login().then((res) => {
                      //     code = res.code;
                      //     return getUserInfo();
                      // }).then((userInfo) => {
                      //     //登录远程服务器
                      //     request(api.AuthLoginByWeixin, {
                      //         code: code,
                      //         userInfo: userInfo
                      //     }, 'POST').then(res => {
                      //         if (res.errno === 0) {
                      //             //存储用户信息
                      //             wx.setStorageSync('userInfo', res.data.userInfo);
                      //             wx.setStorageSync('token', res.data.token);
                      //             resolve(res);
                      //         } else {
                      //             reject(res);
                      //         }
                      //     }).catch((err) => {
                      //         reject(err);
                      //     });
                      // }).catch((err) => {
                      //     reject(err);
                      // })
                  } else {
                      resolve(res.data);
                      wx.hideLoading()
                  }
              } else {
                  reject(res.errMsg);
              }

          },
          fail: function(err) {
              reject(err)
          }
      })
  });
}

//获取用户信息
function GetUserInfo() {
    wx.getUserProfile({
        desc: '获取用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          wx.setStorageSync('userInfo', JSON.stringify(res.userInfo))
          this.UpdateUserInfo(res.userInfo)
          return res.userInfo
        },
        fail:function(err){
          console.log(err)
        }
      })
}



//更新用户信息
function UpdateUserInfo(obj){
    let params = {
      "avatarUrl":obj.avatarUrl,
      "gender":obj.gender,
      "nickName":obj.nickName,
      "openid":wx.getStorageSync('openid'),
    }
    this.request(api.ApiUpdateInfo,params).then(function (res) {
    })
  }

//下载图片
function DownloadFiles(urls){
  this.request(api.ApitPicUrl,{"imgUrl":urls}).then(res=>{})
}
module.exports = {
  formatTime,
  request,
  GetUserInfo,
  UpdateUserInfo,
  DownloadFiles
}
